package com.chang.jason.viewpagerphoto;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jason.Chang on 2018-03-09.
 * explain:
 */

public class MyViewPagerAdapter extends PagerAdapter {
    private Context context;
    private List<String> list;
//    private List<View> lsView=new ArrayList<>();
    private View [] arrayView;

    public MyViewPagerAdapter(Context context,List<String> ls){
        this.context=context;
        this.list=ls;
        arrayView=new View[list.size()];

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        container.removeView(arrayView[position]);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_vp_img,null);
        PhotoView photoView=(PhotoView)view.findViewById(R.id.img);
        Glide.with(context).load(list.get(position)).into(photoView);
        arrayView[position]=view;
        container.addView(view);
        return view;
    }
}
